<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Museumslogs;
use app\common\model\Museumsthird;
use app\common\model\user\Appoints;
use app\common\model\UserCards;
use app\common\model\UserPunch;
use fast\Random;
use think\Log;
use think\Request;

class DataSync  extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $token = $request->header("Authorization", "");
        if ($token !== "101dd5f29337b1a683f56616e2cc7b6e0ff4ebab") {
            $this->error("Token缺失");
        }
    }

    /**
     * 数据同步接口-微信用户
     */
    public function user()
    {
        $nickname = $this->request->param("nickname", "");
        $gender = $this->request->param("gender", "0");
        $avatar = $this->request->param("avatar", "");
        $birthday = $this->request->param("birthday", "");
        $mobile = $this->request->param("mobile", "");
        if (empty($nickname) || empty($mobile)) {
            $this->error("参数不正确");
        }
        $userModel=new \app\common\model\User;
        $user = $userModel->where("username", $mobile)->find();
        if (!empty($user)) {
            $this->success("同步成功", ["id"=>$user->id]);
        }


        $salt = Random::alnum();
        $pwd = $this->auth->getEncryptPassword(Random::alnum(), $salt);

        $saveData = [];
        $saveData['username'] = $mobile;
        $saveData['nickname'] = $nickname;
        $saveData['password'] = $pwd;
        $saveData['salt'] = $salt;
        $saveData['mobile'] = $mobile;
        $saveData['avatar'] = $avatar;
        $saveData['level'] = 0;
        $saveData['gender'] = $gender;
        $saveData['birthday'] = $birthday;
        $saveData['jointime'] = time();
        $saveData['status'] = 'normal';
        $ins_res = $userModel->save($saveData);
        if ($ins_res) {
            $user = $userModel->where("username", $mobile)->find();
            $this->success("同步成功", ["id"=>$user->id]);
        } else {
            $this->error("同步信息失败");
        }
    }


    /**
     * 数据同步接口-微信用户
     */
    public function good()
    {
        $params =$this->request->post();
        $data=(new \app\common\model\Goods())->where(['status'=>1,'id'=>$params['id']])->find();
        if($data){
            $data['goods_image'] =cdnurl($data['goods_image'],true);
            $data['goods_images']=explode(',',$data['goods_images']);
            $goods_images=[];
            foreach ($data['goods_images'] as $k1=>$data1){
                $goods_images[]=cdnurl($data1,true);
            }
            $data['goods_images']=$goods_images;
            $data['goods_sales'] =$data['goods_sales']+$data['base_sales'];
            unset($data['base_sales']);
        }
        $this->success('',$data);
    }

}